﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Base;
using System.ComponentModel;
using Core.Base;
using System.Data;
using Components;

namespace Core.Component
{
    public class SGVTextBoxColumn: DataGridViewTextBoxColumn, IBindingControl, ISGVControl
    {
        BaseEntity entity;
        BaseModule module;
        #region IBindingControl
        String _bindingPropertyName = "Text";

        String _bindingTable;
        String _bindingColumn;
        String _dataSourceTable;
        String _dataSourceDisplayColumn;
        String _dataSourceKeyColumn;
        String _dataSourceColumns;

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        string IBindingControl.BindingPropertyName
        {
            get
            {
                return _bindingPropertyName;
            }
            set
            {
                _bindingPropertyName = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingTable
        {
            get
            {
                return _bindingTable;
            }
            set
            {
                _bindingTable = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingColumn
        {
            get
            {
                return _bindingColumn;
            }
            set
            {
                _bindingColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceTable
        {
            get
            {
                return _dataSourceTable;
            }
            set
            {
                _dataSourceTable = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceDisplayColumn
        {
            get
            {
                return _dataSourceDisplayColumn;
            }
            set
            {
                _dataSourceDisplayColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceKeyColumn
        {
            get
            {
                return _dataSourceKeyColumn;
            }
            set
            {
                _dataSourceKeyColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public String DataSourceColumns
        {
            get
            {
                return _dataSourceColumns;
            }
            set
            {
                _dataSourceColumns = value;
            }
        }

        

        #endregion
        public void RegisterModule(Core.Base.BaseModule _module)
        {
            if (_module != null)
            {
                this.module = _module;
                this.entity = _module.entity;
            }
        }

        public Core.Component.UIComponent component
        {
            set;
            get;
        }

        public void DestrucPropertyName()
        {
            if (String.IsNullOrEmpty(DataPropertyName)) return;
            String strColumnTag = DataPropertyName;
            DataPropertyName = String.Empty;

            String[] lstTags = strColumnTag.Split('|');

            if (lstTags.Length >= 1)
                DataPropertyName = lstTags[0];
            if (lstTags.Length >= 2)
                BindingTable = lstTags[1];
            if (lstTags.Length >= 3)
                DataSourceDisplayColumn = lstTags[2];
            if (lstTags.Length >= 4)
                DataSourceKeyColumn = lstTags[3];
            if (lstTags.Length >= 5)
                DataSourceTable = lstTags[4];

        }

        public void Init()
        {
            this.Frozen = false;
            InitBinding();
            InitDataSource();
        }

        public virtual void InitBinding()
        {
            DestrucPropertyName();
            this.DataPropertyName = BindingColumn;
            if (String.IsNullOrEmpty(DataPropertyName))
                DataPropertyName = this.Name;
        }

        public virtual void InitDataSource()
        {


            //this.Columns
        }

        public string SGVTag
        {
            set;
            get;
        }

        public string SGVShortcutString
        {
            set;
            get;
        }

        public void UpdateFormat()
        {
        }

        public UIComponent Serialize()
        {
            UIComponent component = new UIComponent();
            //SGVControlHelper.CreateUIComponentFromControl(component, this);
            component.strType = this.GetType().Name;
            component.Value = this.HeaderText;
            if (this.HeaderCell.Style.Font != null)
            {
                component.FontStyle = this.HeaderCell.Style.Font.Style;
                component.FontName = this.HeaderCell.Style.Font.Name;
                component.FontSize = this.HeaderCell.Style.Font.Size;
            }

            return component;
        }

        public void Deserialize(UIComponent component)
        {
            throw new NotImplementedException();
        }
    }
}
